<?php

namespace app\admin\controller;

use think\facade\Db;
use think\facade\Request;
use think\facade\View;
use app\admin\model\MessageTpl as messageTplModel;

class MessageTpl extends Common
{

    /**
     * 消息模板列表
     * @author admin
     * @date 2018-04-05
     */
    public function index()
    {
        if (Request::isPost()) {
            $key = input('post.key');
            $page = input('page') ? input('page') : 1;
            $pageSize = input('limit') ? input('limit') : config('pageSize');
            $list = Db::name('MessageTpl')
                ->order('id desc')
                ->paginate(array('list_rows' => $pageSize, 'page' => $page))
                ->toArray();
            return $result = ['code' => 0, 'msg' => '获取成功!', 'data' => $list['data'], 'count' => $list['total'], 'rel' => 1];
        }
        return View::fetch();
    }

    /**
     * 添加消息模板
     * @author admin
     * @date 2018-04-05
     */
    public function add()
    {
        try {
            if ($_POST) {
                $MessageTpl = new messageTplModel();
                $data = Request::only(['id', 'name', 'template_code', 'content', 'type']);
                $id = input("request.id", 0, "intval");
                $result = $MessageTpl->templatesSave($id, $data);
                if ($result) {
                    return $result = ['code' => 0, 'msg' => '操作成功!'];
                }
                return $result = ['code' => 1, 'msg' => '操作失败!'];
            }
            return View::fetch('edit');
        } catch (\Exception $e) {
            return ['code' => 99999, 'msg' => $e->getMessage()];
        }
    }

    /**
     * 编缉消息模板
     * @author admin
     * @date 2018-04-05
     */
    public function edit()
    {
        try {
            if ($_POST) {
                $MessageTpl = new messageTplModel();
                $data = Request::only(['id', 'name', 'template_code', 'content', 'type']);
                $id = input("request.id", 0, "intval");
                $result = $MessageTpl->templatesSave($id, $data);
                if ($result) {
                    return $result = ['code' => 0, 'msg' => '操作成功!'];
                }
                return $result = ['code' => 1, 'msg' => '操作失败!'];
            }

            $id = input('request.id', 0, 'intval');
            if (!$id) {
                return $result = ['code' => 1, 'msg' => '参数错误!'];
            }
            $vo = Db::name('MessageTpl')->find($id);
            $this->assign("info", $vo);
            return View::fetch('edit');
        } catch (\Exception $e) {
            return ['code' => 99999, 'msg' => $e->getMessage()];
        }
    }

    /**
     * 更改状态
     * @param $id 主键
     * @param $status 值
     * @return Array
     */
    public function doStatus()
    {
        try {
            $id = input('request.id');
            $status = input('request.status');
            if (!$id) {
                return ['code' => 3000, 'msg' => '操作失败!'];
            }
            $result = Db::name("message_tpl")->where("id", $id)->update(['status' => $status]);
            if ($result) {
                return ['code' => 0, 'msg' => '操作成功!'];
            }
            return ['code' => 3000, 'msg' => '操作失败!'];
        } catch (\Exception $e) {
            return ['code' => 99999, 'msg' => $e->getMessage()];
        }
    }

    /**
     * 删除数据
     * @param $id 主键
     * @return Array
     */
    public function doDelete()
    {
        try {
            $id = input('request.id');
            if (!$id) {
                return ['code' => 3000, 'msg' => '请选择要删除的数据!'];
            }
            $result = Db::name('message_tpl')->where("id", $id)->delete();
            if ($result) {
                return ['code' => 0, 'msg' => '操作成功!'];
            }
            return ['code' => 3000, 'msg' => '操作失败!'];
        } catch (\Exception $e) {
            return ['code' => 99999, 'msg' => $e->getMessage()];
        }
    }


}
